﻿using System;
using System.Collections.Generic;
using System.Linq;
using ChinaMobile.XinJu;
using Fee.Stores;
using Identity;
using Identity.Stores;

namespace Fee.Sync
{
    public class StationSyncer<T>
        where T : FeeDbContext
    {
        private readonly T _dbContext;

        private readonly IDictionary<string, District> _districtNameMapping;
        private readonly IDistrictStore<T> _districtStore;
        private readonly IStationStore<T> _stationStore;
        private readonly XinJuStationStore _xinJuStationStore;

        public StationSyncer(XinJuStationStore xinJuStationStore, IStationStore<T> stationStore,
            IDistrictStore<T> districtStore, T dbContext)
        {
            _xinJuStationStore = xinJuStationStore;
            _stationStore = stationStore;
            _districtStore = districtStore;
            _dbContext = dbContext;
            _districtNameMapping = districtStore.List().ToDictionary(f => f.Name, f => f);
        }

        public void Sync()
        {
            SyncFainly();
            SyncGroup();
        }

        private void SyncFainly()
        {
            var page = 1;
            var pageSize = 50000;

            var currentCount = 50000;
            while (currentCount == pageSize)
            {
                var familyDataTask = _xinJuStationStore.ListFamily(page, pageSize).Result;

                currentCount = familyDataTask.Count();
                SyncTo(familyDataTask);


                page++;
            }
        }

        private void SyncGroup()
        {
            var page = 1;
            var pageSize = 50000;

            var currentCount = 50000;
            while (currentCount == pageSize)
            {
                var familyDataTask = _xinJuStationStore.ListGroup(page, pageSize).Result;

                currentCount = familyDataTask.Count();
                SyncTo(familyDataTask);


                page++;
            }
        }
        private void SyncTo(IEnumerable<FamilyStation> familyStations)
        {
            foreach (var family in familyStations)
            {

                var st = _stationStore.GetByCode(family.编号, false);
                var changed = false;
                if (st == null)
                {
                    st = new Station { Code = family.编号 };
                    changed = true;
                }

                st.Type = StationType.FamilyCoverage;

                var dist = _districtNameMapping[family.分公司];
                ;
                if (st.District == null || st.District.Id != dist.Id)
                {
                    st.OrderDistrict = dist;
                    st.District = dist;
                    changed = true;
                }

                if (st.ThirdStationStatus != family.当前进度状态)
                {
                    st.ThirdStationStatus = family.当前进度状态;
                    changed = true;
                }

                var userNumber = Convert.ToInt32(String.IsNullOrEmpty(family.户数) ? "0" : family.户数);
                if (st.UsersNumber != userNumber)
                {
                    st.UsersNumber = userNumber;
                    changed = true;
                }

                if (st.Name != family.社区名称)
                {
                    st.Name = family.社区名称;
                    changed = true;
                }


                if (changed) _dbContext.Update(st);
            }

            _dbContext.SaveChanges();
        }

        private void SyncTo(IEnumerable<GroupStation> familyStations)
        {
            foreach (var family in familyStations)
            {
                var st = _stationStore.GetByCode(family.编号, false);
                var changed = false;
                if (st == null)
                {
                    st = new Station { Code = family.编号 };
                    changed = true;
                }
                st.Type = StationType.CustomerGroupCoverage;
                var dist = _districtNameMapping[family.分公司];
                ;
                if (st.District == null || st.District.Id != dist.Id)
                {
                    st.OrderDistrict = dist;
                    st.District = dist;
                    changed = true;
                }

                if (st.ThirdStationStatus != family.当前进度状态)
                {
                    st.ThirdStationStatus = family.当前进度状态;
                    changed = true;
                }
                var userNumber = Convert.ToInt32(String.IsNullOrEmpty(family.户数) ? "0" : family.户数);
                if (st.UsersNumber != userNumber)
                {
                    st.UsersNumber = userNumber;
                    changed = true;
                }

                if (st.Name != family.名称)
                {
                    st.Name = family.名称;
                    changed = true;
                }


                if (changed) _dbContext.Update(st);
            }

            _dbContext.SaveChanges();

        }
    }
}